<html>
<head><meta charset="utf-8"><title>waiting for const generics · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/waiting.20for.20const.20generics.html">waiting for const generics</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="168806173"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/waiting%20for%20const%20generics/near/168806173" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/waiting.20for.20const.20generics.html#168806173">(Jun 23 2019 at 19:59)</a>:</h4>
<p>Preaching to the choir here, but I’m excited for const generics.</p>



<a name="169317876"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/waiting%20for%20const%20generics/near/169317876" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/waiting.20for.20const.20generics.html#169317876">(Jun 30 2019 at 00:28)</a>:</h4>
<p>Can something like this be done with a number of feature flags today?</p>
<div class="codehilite"><pre><span></span><span class="cp">#![feature(const_generics)]</span><span class="w"></span>

<span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="n">mem</span><span class="p">;</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">BplusTreeLeaf</span><span class="o">&lt;</span><span class="n">K</span><span class="p">,</span><span class="w"> </span><span class="n">V</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">PAGE_SIZE</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">keys</span>: <span class="p">[</span><span class="n">K</span><span class="p">;</span><span class="w"> </span><span class="n">PAGE_SIZE</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="p">(</span><span class="n">mem</span>::<span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">K</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">mem</span>::<span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">V</span><span class="o">&gt;</span><span class="p">())],</span><span class="w"></span>
<span class="w">    </span><span class="n">values</span>: <span class="p">[</span><span class="n">V</span><span class="p">;</span><span class="w"> </span><span class="n">PAGE_SIZE</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="p">(</span><span class="n">mem</span>::<span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">K</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">mem</span>::<span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">V</span><span class="o">&gt;</span><span class="p">())],</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="169317991"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/waiting%20for%20const%20generics/near/169317991" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/waiting.20for.20const.20generics.html#169317991">(Jun 30 2019 at 00:32)</a>:</h4>
<p>Even better would be </p>
<div class="codehilite"><pre><span></span><span class="cp">#![feature(const_generics)]</span><span class="w"></span>

<span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="n">mem</span><span class="p">;</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">BplusTreeLeaf</span><span class="o">&lt;</span><span class="n">K</span><span class="p">,</span><span class="w"> </span><span class="n">V</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">PAGE_SIZE</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">keys</span>: <span class="p">[</span><span class="n">K</span><span class="p">;</span><span class="w"> </span><span class="n">Self</span>::<span class="n">CAPACITY</span><span class="p">],</span><span class="w"></span>
<span class="w">    </span><span class="n">values</span>: <span class="p">[</span><span class="n">V</span><span class="p">;</span><span class="w"> </span><span class="n">Self</span>::<span class="n">CAPACITY</span><span class="p">],</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">K</span><span class="p">,</span><span class="w"> </span><span class="n">V</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">PAGE_SIZE</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="n">BplusTreeLeaf</span><span class="o">&lt;</span><span class="n">K</span><span class="p">,</span><span class="w"> </span><span class="n">V</span><span class="p">,</span><span class="w"> </span><span class="p">{</span><span class="n">PAGE_SIZE</span><span class="p">}</span><span class="o">&gt;</span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">CAPACITY</span>: <span class="kt">usize</span> <span class="o">=</span><span class="w"> </span><span class="n">PAGE_SIZE</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="p">(</span><span class="n">mem</span>::<span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">K</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">mem</span>::<span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">V</span><span class="o">&gt;</span><span class="p">());</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>But I know associated consts and array lengths are not the best of friends</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>